var User = require('../model/user')
var Shop = require('../model/shop')
var Goods = require('../model/goods')
var Order = require('../model/order')
var Like = require('../model/like')
var Type = require('../model/content')
var SuccessCode = require('./successCode')
var ErrorCode = require('./errorCode')
var push = require("../push/push")
var time  = require("../time/time")


var orderApi = {
    createOrder: function (req, res) {
        var goods = req.body.goods
        var price = req.body.price
        var shopid = req.body.shopId
        var addrid = req.body.addr
        var userid = req.userid

        if (goods.lenght == 0) {
            res.json(400, ErrorCode.GoodsSizeError)
            return
        }
        if (price < 0) {
            res.json(400, ErrorCode.PriceZreoError)
            return
        }
        Shop.findOne({ _id: shopid }, function (err, shop) {
            if (!shop) {
                res.json(400, ErrorCode.ShopNotExistError)
                return
            }
            if (shop.open) {
                res.json(400, ErrorCode.ShopOffError)
                return
            }
            var t = new Date().getTime()
            var order = {
                goods: goods,
                price: price,
                shop: shopid,
                user: userid,
                addr: addrid,
                status: Type.OrderStatusType.processing,
                time: t,
                end: 0,
                begin: new Date().getTime()
            }
            Order.create(order, function (err, o) {
                if (err) {
                    res.json(400, err)
                    return
                }
                push(userid, shop.user,"收到一条最新的订单")
                res.json(o)
                return
            })

        })
    },
    finishOrder: function (req, res) {
        var orderid = req.body.id
        Order.findOne({ _id: orderid }, function (err, order) {
            if (!order) {
                res.json(400, ErrorCode.CommonError)
                return
            } else {
                order.end = new Date().getTime()
                order.status = Type.OrderStatusType.finish
                order.save(function (err) {
                    if (err) {
                        res.json(400, ErrorCode.CommonError)
                        return
                    } else {
                        res.json(SuccessCode.CommonSuccess)
                        return
                    }
                })
            }
        })
    }
    ,
    failOrder: function (req, res) {
        var orderid = req.body.id
        Order.findOne({ _id: orderid }, function (err, order) {
            if (err) {
                res.json(400, ErrorCode.CommonError)
                return
            } else {
                order.end = new Date().getTime()
                order.status = Type.OrderStatusType.fail
                order.save(function (err) {
                    if (err) {
                        res.json(400, ErrorCode.CommonError)
                        return
                    } else {
                        res.json(SuccessCode.CommonSuccess)
                        return
                    }
                })
            }

        })
    }
    ,
    receiveOrder: function (req, res) {
        var orderid = req.body.id
        var userid = req.userid
        Order.findOne({ _id: orderid }, function (err, order) {
            if (err) {
                res.json(400, ErrorCode.CommonError)
                return
            } else {
                order.status = Type.OrderStatusType.received
                order.save(function (err) {
                    if (err) {
                        res.json(400, ErrorCode.CommonError)
                        return
                    } else {
                        res.json(SuccessCode.CommonSuccess)
                        push(userid,order.user,"商家接受了你的订单")
                        return
                    }
                })
            }

        })
    }
    , listOrderByStatus: function (req, res) {
        var status = req.query.status
        var userid = req.userid

        switch (parseInt(status)) {
            case Type.OrderStatusType.processing:
                Order.find({ user: userid, "$or": [{ status: Type.OrderStatusType.processing }, { status: Type.OrderStatusType.received }] })
                    .populate('goods.goodsId')
                    .populate('shop')
                    .sort('begin')
                    .exec(function (err, orders) {
                        res.json(orders)
                        return
                    })
                break

            case Type.OrderStatusType.finish:
                Order.find({ user: userid, status: Type.OrderStatusType.finish })
                    .populate('goods.goodsId')
                    .populate('shop')
                    .sort('begin')
                    .exec(function (err, orders) {
                        res.json(orders)
                        return
                    })
                break

            case Type.OrderStatusType.fail:
                Order.find({ user: userid, status: Type.OrderStatusType.fail })
                    .populate('goods.goodsId')
                    .populate('shop')
                    .sort('begin')
                    .exec(function (err, orders) {
                        res.json(orders)
                        return
                    })
                break
            case -1:
                Order.find({ user: userid })
                    .populate('goods.goodsId')
                    .populate('shop')
                    .sort('begin')
                    .exec(function (err, orders) {
                        res.json(orders)
                        return
                    })
                break

            default:
                res.json(400, ErrorCode.CommonError)
                return
        }

    },
    listShopOrder: function (req, res) {
        var status = req.query.status
        var userid = req.userid
        Shop.findOne({ user: userid }, function (err, shop) {
            if (!shop) {
                res.json(400, ErrorCode.CommonError)
                return
            } else {
                switch (parseInt(status)) {
                    case Type.OrderStatusType.processing:
                        Order.find({ shop: shop._id, "$or": [{ status: Type.OrderStatusType.processing }, { status: Type.OrderStatusType.received }] })
                            .populate('goods.goodsId')
                            .populate('user')
                            .populate('addr')
                            .sort('begin')
                            .exec(function (err, orders) {
                                res.json(orders)
                                return
                            })
                        break

                    case Type.OrderStatusType.finish:
                        Order.find({ shop: shop._id, status: Type.OrderStatusType.finish })
                            .populate('goods.goodsId')
                            .populate('user')
                            .populate('addr')
                            .sort('begin')
                            .exec(function (err, orders) {
                                res.json(orders)
                                return
                            })
                        break

                    case Type.OrderStatusType.fail:
                        Order.find({ shop: shop._id, status: Type.OrderStatusType.fail })
                            .populate('goods.goodsId')
                            .populate('user')
                            .populate('addr')
                            .sort('begin')
                            .exec(function (err, orders) {
                                res.json(orders)
                                return
                            })
                        break
                    case -1:
                        Order.find({ shop: shop._id })
                            .populate('goods.goodsId')
                            .populate('user')
                            .populate('addr')
                            .sort('begin')
                            .exec(function (err, orders) {
                                res.json(orders)
                                return
                            })
                        break

                    default:
                        res.json(400, ErrorCode.CommonError)
                        return
                }
            }
        })
    },
    listAllOrder: function (req, res) {
        Order.find().populate('goods.goodsId').exec(function (err, orders) {
            res.json(orders)
        })
    },
    listShopOrderAdmin: function (req, res) {
        var status = req.query.status

        switch (parseInt(status)) {
            case Type.OrderStatusType.processing:
                Order.find({ "$or": [{ status: Type.OrderStatusType.processing }, { status: Type.OrderStatusType.received }] }).populate('goods.goodsId').exec(function (err, orders) {
                    res.json(orders)
                    return
                })
                break

            case Type.OrderStatusType.finish:
                Order.find({ status: Type.OrderStatusType.finish }).populate('goods.goodsId').exec(function (err, orders) {
                    res.json(orders)
                    return
                })
                break

            case Type.OrderStatusType.fail:
                Order.find({ status: Type.OrderStatusType.fail }).populate('goods.goodsId').exec(function (err, orders) {
                    res.json(orders)
                    return
                })
                break

            default:
                res.json(400, ErrorCode.CommonError)
                return
        }
    },
    getTodayOrder:function(req,res){
        
        var userid = req.userid
        Shop.findOne({ user: userid }, function (err, shop) {
            if (!shop) {
                res.json(400, ErrorCode.CommonError)
                return
            } else {
                var day = time.getDay()
                Order.find({shop:shop._id})
                .where('end').gt(day.begin).lt(day.end)
                .exec(function(err,orders){
                    if(err){
                        res.json(400,ErrorCode.CommonError)
                    }else{
                        var allPrice = 0
                        for(var i = 0;i<orders.length;i++){
                            allPrice += orders[i].price
                        }
                         var count = orders.length
                        if(!count){
                            count = 0
                        }
                        var body = {
                            count : count,
                            price : allPrice
                        }
                        res.json(body)
                    }
                })
            }
        })
    },
    getMonthOrder:function(req,res){
        
        var userid = req.userid
        Shop.findOne({ user: userid }, function (err, shop) {
            if (!shop) {
                res.json(400, ErrorCode.CommonError)
                return
            } else {
                var month = time.getMonth()
                Order.find({shop:shop._id})
                .where('end').gt(month.begin).lt(month.end)
                .exec(function(err,orders){
                    if(err){
                        res.json(400,ErrorCode.CommonError)
                    }else{
                        var allPrice = 0
                        for(var i = 0;i<orders.length;i++){
                            allPrice += orders[i].price
                        }
                        var count = orders.length
                        if(!count){
                            count = 0
                        }
                        var body = {
                            count : count,
                            price : allPrice
                        }
                        res.json(body)
                    }
                })
            }
        })
    },
    getMonthChartOrder:function(req,res){
        
        var userid = req.userid
        Shop.findOne({ user: userid }, function (err, shop) {
            if (!shop) {
                res.json(400, ErrorCode.CommonError)
                return
            } else {
                var month = time.getMonth()
                Order.find({shop:shop._id})
                .select('-goods -user -addr -shop')
                .where('end').gt(month.begin).lt(month.end)
                .exec(function(err,orders){
                    if(err){
                        res.json(400,ErrorCode.CommonError)
                    }else{
                        res.json(orders)
                    }
                })
            }
        })
    },
    getYearChartOrder:function(req,res){
        var userid = req.userid
        Shop.findOne({ user: userid}, function (err, shop) {
            if (!shop) {
                res.json(400, ErrorCode.CommonError)
                return
            } else {
                Order.find({shop:shop._id,status: Type.OrderStatusType.finish})
                .select('-goods -user -addr -shop')
                .exec(function(err,orders){
                    if(err){
                        res.json(400,ErrorCode.CommonError)
                    }else{
                        res.json(orders)
                    }
                })
            }
        })
    }

    
}
module.exports = orderApi